home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 77 / IOPROG_77.ISO / tips / Java / Bloc Notes / BloccoNotes1.java
Encoding:
Java Source  |  2003-12-19  |  2.9 KB  |  97 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public class BloccoNotes implements WindowListener, ActionListener{
  7.  
  8.   //Istanze della classe
  9.   private Frame finestra;
  10.   private TextArea areaTesto;
  11.  
  12.   //Costruttore della classe
  13.  
  14.    public BloccoNotes(Frame f, TextArea t) {
  15.      finestra = f;
  16.      areaTesto = t;
  17.    }
  18.  
  19.   //Gestione della grafica
  20.  
  21.   // Metodi non implementati dell'interfaccia WindowListener
  22.   public void windowActivated(WindowEvent evt) {}
  23.   public void windowDeactivated(WindowEvent evt) {}
  24.   public void windowDeiconified(WindowEvent evt) {}
  25.   public void windowIconified(WindowEvent evt) {}
  26.   public void windowOpened(WindowEvent evt) {}
  27.  
  28.   // Metodi implementati dell'interfaccia WindowListener
  29.   public void windowClosed(WindowEvent evt) {
  30.     System.exit(0);
  31.   }
  32.  
  33.   public void windowClosing(WindowEvent evt) {
  34.     finestra.dispose();
  35.   }
  36.  
  37.   // Metodo acoltatore ha il computo di ascoltare i comandi
  38.   public void actionPerformed(ActionEvent evt) {
  39.     String comando = evt.getActionCommand();
  40.     //gestione dei vari eventi
  41.     if (comando.equals("Esci")) {
  42.       finestra.dispose();
  43.     } else if (comando.equals("Apri")) {
  44.       areaTesto.setText("");
  45.       FileDialog d = new FileDialog(finestra, "Apri documento", FileDialog.LOAD);
  46.       d.setVisible(true);
  47.       if (d.getFile() != null) {
  48.         try {
  49.           String line;
  50.           BufferedReader in = new BufferedReader(new FileReader(d.getDirectory() + File.separator + d.getFile()));
  51.           while ((line = in.readLine()) != null)
  52.             areaTesto.append(line + "\n");
  53.         } catch (Exception e) {
  54.           System.out.println("Errore: " + e);
  55.         }
  56.       }
  57.     } else if (comando.equals("Salva con nome...")) {
  58.       FileDialog d = new FileDialog(finestra, "Salva documento con nome", FileDialog.SAVE);
  59.       d.setVisible(true);
  60.       PrintWriter fout;
  61.       String Stringa=areaTesto.getText();
  62.       try{
  63.         fout=new PrintWriter(new FileWriter(d.getDirectory()+File.separator+d.getFile()));
  64.         StringTokenizer st=new StringTokenizer(Stringa,"\n"); //E' importante per gestire la pressione dell'invio
  65.         while (st.hasMoreTokens())
  66.           fout.println(st.nextToken());
  67.         fout.close();
  68.       }catch(Exception e){}
  69.   }
  70. }
  71.  
  72.   /** Metodo Main */
  73.   public static void main(String[] args) {
  74.     Frame f = new Frame("My blocco note ");
  75.     TextArea t = new TextArea();
  76.     BloccoNotes n = new BloccoNotes(f, t);
  77.     MenuBar mb = new MenuBar();
  78.  
  79.     // Preparazione men∙ file il menu File
  80.     Menu menu = new Menu("File");
  81.     mb.add(menu);
  82.     menu.add("Apri");
  83.     menu.add("Salva con nome...");
  84.     menu.addSeparator();
  85.     menu.add("Esci");
  86.  
  87.     menu.addActionListener(n);
  88.  
  89.     // Prepara la finestra
  90.     f.addWindowListener(n);
  91.     f.setMenuBar(mb);
  92.     f.add(t);
  93.     f.setSize(400, 500);
  94.     f.setVisible(true);
  95.   }
  96. } // BloccoNotes
  97.